<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Labor;
use App\Models\LaborAttendance;
use App\Models\LaborTeam;
use App\Models\Project;
use App\Services\LaborAttendanceService;
use App\Services\TencentFaceService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LaborAttendanceController extends Controller
{
    protected $LaborAttendanceService;
    public function __construct(LaborAttendanceService $LaborAttendanceService)
    {
        $this->LaborAttendanceService = $LaborAttendanceService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $attendance_date = Date("Y-m-d");
        $sql = $this->LaborAttendanceService->get('');
        $data = $sql->orderBy('id','desc')->where('attendance_date',$attendance_date)->where('project_id',$request->project_id)->where('team_id',$request->team_id)->get();
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        if(!$request->number){
            return response()->json(['code' => '403', 'message' => '请选择项目']);
        }
        // if(!$request->team_id){
        //     return response()->json(['code' => '403', 'message' => '请选择班组']);
        // }
        if(!$request->mobile){
            return response()->json(['code' => '403', 'message' => '请输入手机号码']);
        }
        if(!$request->id_card){
            return response()->json(['code' => '403', 'message' => '请输入身份证号码']);
        }
        if(!$request->bank_number){
            return response()->json(['code' => '403', 'message' => '请输入银行卡号码']);
        }

        //根据项目编号查询项目
        $project = Project::where('number',$request->number)->first();
        if(!$project){
            return response()->json(['code' => 404, 'message' => '该项目不存在', 'data'=>null]);
        }

        // $team = LaborTeam::find($request->team_id);
        // if(!$team){
        //     return response()->json(['code' => 404, 'message' => '请选择正确的班组', 'data'=>null]);
        // }

        $data = Labor::where('company_id',$project->company_id)->where('id_card',$request->id_card)->first();
        if($data){
            //判断是否拉黑
        }
        if(!$data){
            $data = new Labor();
        }
        $data->name = $request->name;
        $data->mobile = $request->mobile;
        $data->project_id = $project->id;
        $data->company_id = $project->company_id;
        $data->id_card = $request->id_card;
        $data->bank = $request->bank;
        $data->bank_number = $request->bank_number;
        $data->line_number = $request->line_number;
        $data->bank_deposit = $request->bank_deposit;
        $data->emergency_contact = $request->emergency_contact;
        $data->emergency_mobile = $request->emergency_mobile;
        $data->photo = $request->photo;
        $data->id_card_personal = $request->id_card_personal;
        $data->id_card_emblem = $request->id_card_emblem;
        $data->work = $request->work;
        $data->age = $request->age;
        $data->nationality = $request->nationality;
        $data->sex = $request->sex;
        $data->birth = $request->birth;
        $data->address = $request->address;
        $data->home_address = $request->home_address;
        $data->modality = $request->modality;
        $data->save();
        //判断权限
        $data->token = $data->createToken('labor_api')->accessToken;
        $data->type = 'labor-refresh';
        // $data->team_id = $request->team_id;
        return response()->json(['code' => 200, 'message' => '操作成功', 'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //根据项目编号查询项目
        $team = LaborTeam::find($request->team_id);
        if(!$team){
            return response()->json(['code' => 404, 'message' => '请选择正确的班组', 'data'=>null]);
        }


        $POSTONLY = LaborAttendance::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['project_id'] = $request->project_id;
        $input['labor_id'] = Auth::user()->id;

        $situation = $this->LaborAttendanceService->situation($input);
        if($situation == true){
            $data = $this->LaborAttendanceService->attendance($input);
        }
        return response()->json(['code' => 200, 'message' => '操作成功', 'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function face(Request $request)
    {
        if(!$request->photo){
            return response()->json(['code' => 404, 'message' => '请重新进行人脸验证', 'data'=>null]);
        }

        //根据项目编号查询项目
        $team = LaborTeam::find($request->team_id);
        if(!$team){
            return response()->json(['code' => 404, 'message' => '请选择正确的班组', 'data'=>null]);
        }

        $user = Auth::user();
        $POSTONLY = LaborAttendance::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['project_id'] = $request->project_id;
        $input['labor_id'] = $user->id;
        $TencentFaceService = new TencentFaceService();
        if(strpos($user->photo, 'https') === false){
            $user->photo = env('UPLOAD_APP_PREFIX').$user->photo;
        }
        $FaceSimilarity = $TencentFaceService->FaceSimilarity($request->photo,$user->photo);
        $data = '';
        if($FaceSimilarity->Score > 70){
            $data = $this->LaborAttendanceService->attendance($input);
        }else{
            return response()->json(['code' => 500, 'message' => '非本人，无法打卡']);
        }
        return response()->json(['code' => 200, 'message' => '打卡成功', 'FaceSimilarity'=>$FaceSimilarity, 'data'=>$data]);
    }
}
